<?php
switch($action){
	/************************************** manager/search BEGIN ************************************************/
	case "manager":
	case "search":
	require_once('../inc/pager.php');

echo <<<EOT
	<div id="smalltab_container"></div>
	<div class="smalltab_line"></div>
	<div id="t1">
EOT;
	if($action=="manager"){
echo <<<EOT
	<form onsubmit="return false;">
	<div class="div_clear" style="height:10px;"></div>
	<div class="tips_1"><b>提示</b><br />
	* 请先根据条件搜索附件，然后选择相应的操作。<br />
	</div>
	<div class="div_clear" style="height:10px;"></div>
	<table class="table_1">
		<tr><td class="td_0">搜索已丢失的附件:</td><td class=""></td></tr>
		<tr><td class="td_1"><input type="radio" value="1" name="attlost" class="radio_css" /> 是 &nbsp; &nbsp;<input type="radio" value="0" name="attlost" checked="true" class="radio_css" /> 否</td><td class="td_2">数据库存在附件记录，但是文件已经被删除</td></tr>
		<tr><td class="td_0">所在版块:</td><td class=""></td></tr>
		<tr><td class="td_1"><select id="fid" name="fid"><option value="0">所有版块</option>{$cache_forumsoption2}</select></td><td class="td_2">可使用通配符 *</td></tr>
		<tr><td class="td_0">附件尺寸范围:(字节)</td><td class=""></td></tr>
		<tr><td class="td_1"><input type="text" value="" name="size_1" id="size_1" size="10" class="text_css" /> ~ <input type="text" value="" name="size_2" id="size_2" size="10" checked="true" class="text_css" /></td><td class="td_2"></td></tr>
		<tr><td class="td_0">下载次数范围:</td><td class=""></td></tr>
		<tr><td class="td_1"><input type="text" value="" name="downloadnum_1" id="downloadnum_1" size="10" class="text_css" /> ~ <input type="text" value="" name="downloadnum_2" id="downloadnum_2" size="10" class="text_css" /></td><td class="td_2"></td></tr>
		<tr><td class="td_0">上传时间范围:</td><td class=""></td></tr>
		<tr><td class="td_1"><input type="text" value="" name="uploadtime_1" id="uploadtime_1" size="10" class="text_css" /> ~ <input type="text" value="" name="uploadtime_2" id="uploadtime_2" size="10" class="text_css" /></td><td class="td_2"></td></tr>
		<tr><td class="td_0">文件名:</td><td class=""></td></tr>
		<tr><td class="td_1"><input type="text" size="28" id="filename" name="filename" class="text_css" /></td><td class="td_2"></td></tr>
		<tr><td class="td_0">上传者:</td><td class=""></td></tr>
		<tr><td class="td_1"><input type="text" size="28" id="username" name="username" class="text_css" /></td><td class="td_2"></td></tr>
		<tr><td class="td_3"><input class="button_css" type="button" value="  搜 索  " onclick="searchattachment()" /></td><td class=""></td></tr>
	</table>
	</form>
EOT;
	 }else{
	 	$dwidth=array(0,35,250,130,200,100,70,80);
		$attlost=numFilter($_GET['attlost']);
		$fid=numFilter($_GET['fid']);
		$size_1=numFilter($_GET['size_1']);
		$size_2=numFilter($_GET['size_2']);
		$downloadnum_1=strFilter($_GET['downloadnum_1']);
		$downloadnum_2=strFilter($_GET['downloadnum_2']);
		$uploadtime_1=strFilter($_GET['uploadtime_1']);
		$uploadtime_2=strFilter($_GET['uploadtime_2']);
		$uploadtime_1_int = empty($uploadtime_1) ? 0 : (strtotime($uploadtime_1)-$cache_settings['timeoffset']*3600);
		$uploadtime_2_int = empty($uploadtime_2) ? 0 : (strtotime($uploadtime_2)-$cache_settings['timeoffset']*3600);

		$filename=strFilter($_GET['filename']);
		$filename=str_replace("*","%",$filename);
		$filename=$filename=="%"?"":$filename;

		$username=strFilter($_GET['username']);
		$username=str_replace("*","%",$username);
		$username=$username=="%"?"":$username;

		$cond="attachments.tid=topics.id";
		$cond.=($fid>0?" and topics.fid ={$fid}":"");
		$cond.=($size_1>0?" and attachments.filesize>={$size_1}":"");
		$cond.=($size_2>0?" and attachments.filesize<={$size_2}":"");
		$cond.=($downloadnum_1>0?" and attachments.downloadnum>={$downloadnum_1}":"");
		$cond.=($downloadnum_2>0?" and attachments.downloadnum<={$downloadnum_1}":"");
		$cond.=($uploadtime_1_int>0?" and attachments.uploadtime>={$uploadtime_1_int}":"");
		$cond.=($uploadtime_2_int>0?" and attachments.uploadtime<={$uploadtime_2_int}":"");

		$cond.=(empty($filename) ?"":" and attachments.filename like '%{$filename}%'");
		$cond.=(empty($username) ?"":" and attachments.username='{$username}'");
		
		$curPage = numFilter($_GET["page"]);
		$pager = new Pager();
		$pager->init(5,$curPage,"admin.php?inc=attachment&action=search&username={$username}&filename={$filename}&attlost={$attlost}&fid={$fid}&size_1={$size_1}&size_2={$size_2}&downloadnum_1={$downloadnum_1}&downloadnum_2={$downloadnum_2}&uploadtime_1={$uploadtime_1}&uploadtime_2={$uploadtime_2}&page={page}");
		$rows = $pager->queryRows($db,"attachments,topics", $cond, "*,attachments.id as aid,attachments.username as username,attachments.userid as userid","attachments.tid desc,attachments.id desc");
		
echo <<<EOT
	<div class="div_clear" style="height:10px;"></div>
	<div class="tips_1">共搜索到<b>{$pager->recordNum}</b>个符合条件的记录。<a href="admin.php?inc=attachment&action=manager">»返回重新搜索</a><br /></div>
EOT;
	echo("<form id=\"attachmentform\" onsubmit=\"return false;\">");
	echo("<table class=\"table_1\" width=\"100%\">");
	echo("<tr style=\"font-weight:bold;color:#333333;\"><td class=\"row_0\"><div class=\"rowdiv_0\" style=\"width:{$dwidth[1]}px;\"><span class=\"warning\">删除</span></div>"
	."<div class=\"rowdiv_0\" style=\"width:{$dwidth[2]}px;\">文件名/路径</div>"
	."<div class=\"rowdiv_0\" style=\"width:{$dwidth[3]}px;\">作者/上传时间</div>"
	."<div class=\"rowdiv_0\" style=\"width:{$dwidth[4]}px;padding:0 10px 0 0;\">所在主题</div>"
	."<div class=\"rowdiv_0\" style=\"width:{$dwidth[5]}px;\">文件大小</div>"
	."<div class=\"rowdiv_0\" style=\"width:{$dwidth[6]}px;\">下载次数</div>"
	."<div class=\"rowdiv_0\" style=\"width:{$dwidth[7]}px;\">状态</div></td></tr>");
	for($i=0;$i<count($rows);$i++){
		$row=$rows[$i];
		$row['title']=htmlFilter($row['title']);
		$row['username']=htmlFilter($row['username']);
		$row['filesize']=sizeFormat($row['filesize'],"KB");
		$row['uploadtime']=getDateStr($row['uploadtime']);
		$filepath="../uploadfile/attachment/".$row['filepath'];

		if(file_exists($filepath)){
			if($attlost==1){
				continue;
			}
			$static="<a href=\"../attachment.php?id={$row['aid']}\" target=\"_blank\">正常</a>";
		}else{
			$static="<span style='color:#ff3300'>丢失</span>";
		}
		echo("<tr><td class=\"row_0\" style=\"line-height:150%;\"><div class='rowdiv_0' style='width:{$dwidth[1]}px;'><input type=\"checkbox\" value=\"{$row['aid']}\" name=\"delaid[]\" class=\"checkbox_css\" /></div>"
		."<div class='rowdiv_0' style='width:{$dwidth[2]}px;'>{$row['filename']}<br /><a href=\"../attachment.php?id={$row['aid']}\" target=\"_blank\" style=\"font-size:10px;\">{$row['filepath']}</a></div>"
		."<div class='rowdiv_0' style='width:{$dwidth[3]}px;'><a href=\"../user.php?uid={$row['userid']}\" target=\"_blank\">{$row['username']}</a><br /><span class=\"time\">{$row['uploadtime']}</span></div>"
		."<div class='rowdiv_0' style='width:{$dwidth[4]}px;padding:0 10px 0 0;'><a href=\"../view.php?tid={$row['tid']}\" target=\"_blank\">{$row['title']}</a></div>"
		."<div class='rowdiv_0' style='width:{$dwidth[5]}px;'>{$row['filesize']}</div>"
		."<div class='rowdiv_0' style='width:{$dwidth[6]}px;'>{$row['downloadnum']}</div>"
		."<div class='rowdiv_0' style='width:{$dwidth[7]}px;'>{$static}</div></td></tr>");				
	}
	echo("</table>");
	echo("</form>");
	echo("<table width=100%><tr><td><input type=\"checkbox\" onclick=\"selectall(this.checked)\" class=\"checkbox_css\" /> 全选 &nbsp;  &nbsp; <input class=\"button_css\" type=\"button\" value=\"  删除所选  \" onclick=\"ajax_delattachment_yn()\" /></td><td><div class='pagestrdiv'>{$pager->getPageStr()}</div></td></tr></table>");

 	}
echo <<<EOT
	
	</div>
	<div id="t2"></div>
	<div id="t3"></div>
	<div class="div_clear" style="height:30px;"></div>
<script>
var smallNowTab;
var pt = new Tabs();
pt.classpre="smalltab_";
pt.container = "smalltab_container";
pt.createTab("t1","附件管理","",true,"n");
//pt.createTab("t2","批量删除","",false,"n");
pt.init = function(){
	smallNowTab = pt.nowTab;
};
pt.onclick = function(){
	smallNowTab = pt.nowTab;
};		
pt.initTab();
pt.clickNowTab();

function selectall(b){
	var oForm=getE("attachmentform");
	for (var i = 0; i < oForm.elements.length; i++) {
		if(oForm.elements[i].type=="checkbox"&&!oForm.elements[i].disabled){
			oForm.elements[i].checked = b;
		}
	}
}

function searchattachment(){
	var loc = "admin.php?inc=attachment&action=search&attlost="+getRadioValue("attlost")+"&fid="+E("fid").value+"&username="+E("username").value+"&size_1="+E("size_1").value+"&size_2="+E("size_2").value+"&downloadnum_1="+E("downloadnum_1").value+"&downloadnum_2="+E("downloadnum_2").value+"&uploadtime_1="+E("uploadtime_1").value+"&uploadtime_2="+E("uploadtime_2").value+"&filename="+E("filename").value+"&username="+E("username").value;
	self.location.href = loc;
}

function ajax_delattachment_yn(){
	var btns=[
		{value:" 确 认 ",onclick:"mainifm.ajax_delattachment()",focus:true},
		{value:" 取 消 ",onclick:"popwin.close()"}
	];
	popwin.showDialog(3,"确认","该操作会删除所选附件，并不可恢复。<br />是否确认删除？",btns,340,130);
}

function ajax_delattachment(){
	popwin.loading();
	ajaxPost("attachmentform","attachment_ajax.php?action=delattachment",ajax_delattachment_callback);
}
function ajax_delattachment_callback(data){
	var btns=[{value:" 确 定 ",onclick:"popwin.close();mainifm.location.reload();",focus:true}];
	popwin.loaded();
	if(data=="y"){
		popwin.showDialog(1,"删除成功","附件删除成功",btns,280,130);
	}else{
		popwin.showDialog(0,"操作失败","操作失败:<br />"+data,btns,280,130);
	}
}

function PageInit(){
	if(E("uploadtime_1")){E("uploadtime_1").onfocus = function(){choosedate.dfd(E('uploadtime_1'))};}
	if(E("uploadtime_2")){E("uploadtime_2").onfocus = function(){choosedate.dfd(E('uploadtime_2'))};}
}

function checkKeyPressEnter(eventobject){
	var eve=eventobject||window.event;
	if(eve.keyCode==13) {
		searchattachment();
	}
}
window.onload=PageInit;

</script>
EOT;
	break;
	/**************************************  manager/search END ************************************************/

	/************************************** settings BEGIN ************************************************/
	case "settings":
	$row=getSettings();
echo <<<EOT
	<div id="smalltab_container"></div>
	<div class="smalltab_line"></div>
	<div id="t1">
		<form id="settingsform" onsubmit="return false;">
		<div class="div_clear" style="height:10px;"></div>
		<table class="table_1">
			<tr><td class="td_0">帖子中显示图片附件:<span class='required'>(*)</span></td><td class=""></td></tr>
			<tr><td class="td_1"><input type="radio" value="1" name="settings[isshowimg]" class="radio_css" /> 是 &nbsp; &nbsp;<input type="radio" value="0" name="settings[isshowimg]" class="radio_css" /> 否</td><td class="td_2">是否在帖子中直接显示图片附件</td></tr>
			<tr><td class="td_0">帖子中直接播放媒体文件:<span class='required'>(*)</span></td><td class=""></td></tr>
			<tr><td class="td_1"><input type="radio" value="1" name="settings[isplaymedia]" class="radio_css" /> 是 &nbsp; &nbsp;<input type="radio" value="0" name="settings[isplaymedia]" class="radio_css" /> 否</td><td class="td_2">是否在帖子中直接显示图片附件</td></tr>
			<tr><td class="td_0">附件保存方式:</td><td class=""></td></tr>
			<tr><td class="td_1"><select name="settings[attachmentsave]" id="settings[attachmentsave]"><option value="all">全部存入同一目录</option><option value="month">按月份存入不同的目录</option><option value="day">按天存入不同的目录</option><option value="ext">按文件类型存入不同的目录</option></select></td><td class="td_2">本设置只影响新上传的附件，设置更改之前的附件仍存放在原来位置。</td></tr>
			<tr><td class="td_3"><input class="button_css" type="button" value="  提  交  " onclick="ajax_savesettings()" /></td><td class=""></td></tr>
		</table>
		</form>
	</div>
	<div id="t2"></div>
	<div id="t3"></div>
	<div class="div_clear" style="height:30px;"></div>
	<script>
	var smallNowTab;
	var pt = new Tabs();
	pt.classpre="smalltab_";
	pt.container = "smalltab_container";
	pt.createTab("t1","附件设置","",true,"n");
	pt.init = function(){
		smallNowTab = pt.nowTab;
	};
	pt.onclick = function(){
		smallNowTab = pt.nowTab;
	};		
	pt.initTab();
	pt.clickNowTab();
	
	var isshowimg="{$row['isshowimg']}";
	setRadioCheck("settings[isshowimg]",isshowimg);
	
	var isplaymedia="{$row['isplaymedia']}";
	setRadioCheck("settings[isplaymedia]",isplaymedia);
	
	var attachmentsave="{$row['attachmentsave']}";
	setSelect("settings[attachmentsave]",attachmentsave);
	
	function ajax_savesettings(){
		popwin.loading();
		ajaxPost("settingsform","attachment_ajax.php?action=savesettings",ajax_savesettings_callback);
	}
	function ajax_savesettings_callback(data){
		var btns=[{value:" 确 定 ",onclick:"popwin.close();",focus:true}];
		popwin.loaded();
		if(data=="y"){
			popwin.showDialog(1,"修改成功","附件设置修改成功",btns,280,130);
		}else{
			popwin.showDialog(0,"操作失败","操作失败:<br />"+data,btns,280,130);
		}
	}
	
	</script>
EOT;
	break;
	/************************************** settings END ************************************************/


}	
?>